package com.bjSoft.regressionTestTool.config.ui;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.bjSoft.regressionTestTool.Activator;
import com.bjSoft.regressionTestTool.config.ConfigFileHandler;

public class ConfigDialog extends Dialog {

	private Text eclipseLocation;
	private Text reportToolText;
	private Text executionToolText;
	private Text instrummentToolText;
	
	private IProject project;
	
	/**
	 * Create the dialog
	 * @param parentShell
	 */
	public ConfigDialog(Shell parentShell) {
		super(parentShell);
	}

	/**
	 * Create contents of the dialog
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		container.setLayout(gridLayout);

		final Label instrumentationToolLocationLabel = new Label(container, SWT.NONE);
		instrumentationToolLocationLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		instrumentationToolLocationLabel.setText("Instrumentation Tool Location:");

		instrummentToolText = new Text(container, SWT.BORDER);
		instrummentToolText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		instrummentToolText.setText(Activator.instrumentToolLocation);
		final Button instrFolderButton = new Button(container, SWT.NONE);
		instrFolderButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				FileDialog fileDialog = new FileDialog(getShell());
				String path = fileDialog.open();
				instrummentToolText.setText(path);
			}
		});
		instrFolderButton.setText("...");

		final Label executionToolLocationLabel = new Label(container, SWT.NONE);
		executionToolLocationLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		executionToolLocationLabel.setText("Execution Tool Location:");

		executionToolText = new Text(container, SWT.BORDER);
		executionToolText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		executionToolText.setText(Activator.executionToolLocation);
		
		final Button execFolderButton = new Button(container, SWT.NONE);
		execFolderButton.setText("...");
		execFolderButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				FileDialog fileDialog = new FileDialog(getShell());
				String path = fileDialog.open();
				executionToolText.setText(path);
			}
		});

		final Label reportToolLocationLabel = new Label(container, SWT.NONE);
		reportToolLocationLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		reportToolLocationLabel.setText("Report Tool Location:");

		reportToolText = new Text(container, SWT.BORDER);
		reportToolText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		reportToolText.setText(Activator.reportToolLocation);
		
		final Button reportFolderButton = new Button(container, SWT.NONE);
		reportFolderButton.setText("...");
		
		reportFolderButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				FileDialog fileDialog = new FileDialog(getShell());
				String path = fileDialog.open();
				reportToolText.setText(path);
			}
		});

		final Label eclipseLocationLabel = new Label(container, SWT.NONE);
		eclipseLocationLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		eclipseLocationLabel.setText("Eclipse Location:");

		eclipseLocation = new Text(container, SWT.BORDER);
		eclipseLocation.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		eclipseLocation.setText(Activator.eclipseLocation);
		
		final Button eclipseLocationButton = new Button(container, SWT.NONE);
		eclipseLocationButton.setText("...");
		eclipseLocationButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog dd = new DirectoryDialog(getShell());
				String path = dd.open();
				eclipseLocation.setText(path);
			}
		});
		
		//
		return container;
	}

	/**
	 * Create contents of the button bar
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(421, 200);
	}
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Regression Test Configuration");
		
	}
	@Override
	public void okPressed() {
		if (this.instrummentToolText.getText() != null 
				&& !this.instrummentToolText.getText().equals("")) {
			Activator.instrumentToolLocation = this.instrummentToolText.getText();
		}
		if (this.executionToolText.getText() != null
				&& !this.executionToolText.equals("")) {
			Activator.executionToolLocation = this.executionToolText.getText();
		}
		if (this.reportToolText.getText() != null 
				&& !this.reportToolText.getText().equals("")) {
			Activator.reportToolLocation = this.reportToolText.getText();
		}
		if (this.eclipseLocation.getText() != null
				&& !this.eclipseLocation.getText().equals("")) {
			Activator.eclipseLocation = this.eclipseLocation.getText();
		}
		ConfigFileHandler.writeConfigFile(ResourcesPlugin.getWorkspace().getRoot());
		this.close();
	}
	public void cancelPressed() {
		this.close();
	}

}
